package Thread2;

import java.util.Random;

public class homework3 {
    public static long sum1 = 0;
    public static long sum2 = 0;
    public static void main(String[] args) throws InterruptedException {
        long begin = System.nanoTime();
        int len = 1000 * 10000;
        int[] arr = new int[len];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(100) + 1;
        }

        Thread t1 = new Thread(()->{
            for (int i = 0; i < arr.length; i+=2) {
                sum1 += arr[i];
            }
        });

        Thread t2 = new Thread(()->{
            for (int i = 1; i < arr.length; i+=2) {
                sum2 += arr[i];
            }
        });
        t1.start();
        t1.join();
        t2.start();
        t2.join();
        long sum = sum1 + sum2;
        System.out.println(sum);
        long end = System.nanoTime();
        double ms = (end - begin) * 1.0 / 1000 / 1000;
        System.out.println("程序耗时: "+ ms + "毫秒");
    }


}
